var ns, toast, actionsheet, UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        defaultProps: {
            id: 'id',             // 此项为id项的key
            children: 'children', // 此项为修改子集数据的key
            label: 'label' // 此项为修改显示数据的key
        },
        choose_sortList:[],
        list:[],
        divider: false,
        edit: true,
        showAll:true,
        showMenu:true,
        showAdd:false,
    },
    methods: {
        get_data:function () {
            var that=this;
            var option = {};
            option.action = "getsearcherproductlistparams";
            ns.post(option, function(succeed, data, err) {
                if (succeed) {
                    const List=data.CategoryList;
                    if(that.choose_sortList!=undefined&&that.choose_sortList.length>0){
                        that.find_product(that.choose_sortList,List);
                    }
                    that.list = that.buildTree(List, 0, "ID", "ParentID");
                    console.log(JSON.stringify(that.list));

                    
                    
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        buildTree:function(arr, root, key, pKey) {
            var that=this;
            const temp = [];
            for (const item of arr) {
                if (Number(item[pKey]) === Number(root)) {
                    const children = that.buildTree(arr, item[key], key, pKey);
                    if (children.length > 0) item.children = children
                    temp.push(item);
                }
            }
            return temp;
        },
        //展开全部
        do_openall:function(){
            var that=this;
            that.showAll=!that.showAll;
        },
        do_opentype:function(){
            var that=this;
            that.showMenu=!that.showMenu;
        },
        choose_sort:function(item){
            var that=this;
            item.IsSelected=!item.IsSelected;
            if(item.IsSelected==false){
                that.choose_sortList.push(item); 
            }else if(item.IsSelected==true){
                let menu=[]; 
                menu=that.choose_sortList.filter((item2)=>item2.IsSelected!=true&&item2.ID!=item.ID);
                that.choose_sortList=menu;
            }
            console.log(JSON.stringify(that.choose_sortList));
        },
        do_save:function(){
            var that=this;
            api.sendEvent({
                name: 'do_add_sortproduct',
                extra: {
                    list: that.choose_sortList,
                }
            });
            api.closeWin();
            
        },
        find_product:function(list,allist){
            var that=this;
            for(let item of allist){
               const flag=list.some(function matcher(p){
                    return p.ID==item.ID;
                });
                item.IsSelected=!flag;
            }
            // console.log(JSON.stringify(that.list));
            // that.productList.map(res=>{
            //     const {ID} = res
            //     const has = list.find(r=> Number(r.ID) === Number(ID))
            //     res.isSelect = has !== undefined
            //     return res
            // })
        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    // toast = new auiToast();
    // actionsheet = new auiActionsheet();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    app.choose_sortList=api.pageParam.list;
    console.log(JSON.stringify(app.choose_sortList));
    
    app.get_data();

};
